﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CubicFWNet.Data.Models;
using CubicFWNet.Data.SystemManager;
using CubicFWNet.Data.ViewModal;

namespace CubicFWNet.Application.SystemManager
{
    public class InvoiceApp
    {
        private InvoiceRepository _service = new InvoiceRepository();
        private DeliveryProductRepository _serviceDeliveryProduct = new DeliveryProductRepository();
        private InvoceTypeRepository _serviceInvoceType = new InvoceTypeRepository();
        private SendCarRepository _serviceSendCar = new SendCarRepository();
        private ExpressOrderRepository _serviceExpressOrder = new ExpressOrderRepository();
        private InvoiceOtherRepository _serviceInvoiceOther = new InvoiceOtherRepository();
        private CollectAndPayMentRepository _serviceCollectAndPayMent = new CollectAndPayMentRepository();

        public int Add(Invoice web)
        {
            return _service.Insert(web);
        }

        public int Update(Invoice web)
        {
            return _service.Update(web);
        }

        public Invoice GetSingleInvoice(Guid id)
        {
            return _service.FindEntity(a => a.Id == id);
        }

        public IEnumerable<Invoice> GetInvoiceList(string number, string takeCompany)
        {
            return _service.IQueryable(a => a.Number.Contains(number) && a.TakeCompany.Contains(takeCompany)).OrderByDescending(a => a.CreateTime);
        }
        public IEnumerable<Invoice> GetInvoiceListByStatus(int status, string number, string takeCompany)
        {
            return _service.IQueryable(a => (a.Status == status || (status == -1 && (a.Status == -1 || a.Status == 1))) && a.Number.Contains(number) && a.TakeCompany.Contains(takeCompany)).OrderByDescending(a => a.CreateTime);
        }

        public InvoiceView GetSingleInvoiceView(Guid id)
        {
            return _service.GetSingleInvoiceView(id);
        }
        public InvoiceView GetSingleInvoiceView1(Guid id)
        {
            return _service.GetSingleInvoiceView1(id);
        }
        public int UpdateSendCar(SendCar web)
        {
            return _serviceSendCar.Update(web);
        }

        public IEnumerable<InvoiceView> SendCarQuery(int type, string number, DateTime beginTime, DateTime endTime)
        {
            return _service.SendCarQuery(type, number, beginTime, endTime).OrderBy(a=>a.Status).ThenByDescending(a => a.ApplyTime);
        }

        public IEnumerable<InvoiceView> ExpressOrderQuery(int type, string number, DateTime beginTime, DateTime endTime)
        {
            return _service.ExpressOrderQuery(type, number, beginTime, endTime).OrderByDescending(a => a.ApplyTime);
        }

        public IEnumerable<Invoice> GetInvoiceList1(string number, string takeCompany)
        {
            return _service.IQueryable(a => a.Number.Contains(number) && a.TakeCompany.Contains(takeCompany)).OrderByDescending(a => a.CreateTime);
        }

        /// <summary>
        /// 状态大于2小于10 为自己定的规则
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public IEnumerable<Invoice> GetInvoiceListByOrderId(Guid orderId)
        {
            return _service.IQueryable(a => a.OrderId == orderId && a.Status >= 2 && a.Status != 101).OrderByDescending(a => a.CreateTime);
        }

        /// <summary>
        /// 根据订单号来查询发货单
        /// </summary>
        /// <param name="orderNumber">订单号</param>
        /// <returns></returns>
        public IEnumerable<InvoiceView> GetInvoiceListGroupByOrder(string orderNumber)
        {
            return _service.GetInvoiceListGroupByOrder(orderNumber).OrderByDescending(a => a.CreateTime);
        }

        public IEnumerable<Invoice> GetInvoiceListTemp(string number, string takeCompany, int status)
        {
            return _service.IQueryable(a => a.Status >= status && a.Number.Contains(number) && a.TakeCompany.Contains(takeCompany)).OrderByDescending(a => a.CreateTime);
        }

        public IEnumerable<InvoceType> GetInvoceTypeList()
        {
            return _serviceInvoceType.IQueryable().OrderByDescending(a => a.Id);
        }
        public InvoceType GetSingleInvoceType(int typeId)
        {
            return _serviceInvoceType.FindEntity(a => a.Id == typeId);
        }

        public InvoiceOther GetSingleInvoiceOther(Guid id, int type)
        {
            return _serviceInvoiceOther.FindEntity(a => a.InvoicId == id && a.Type == type);
        }

        public CollectAndPayMent GetSingleCollectAndPayMent(Guid invoicId, Guid orderId)
        {
            return _serviceCollectAndPayMent.FindEntity(a => a.InvoicId == invoicId && a.OrderId == orderId);
        }

        public int AddInvoiceOther(InvoiceOther web)
        {
            return _serviceInvoiceOther.Insert(web);
        }
        public int AddCollectAndPayMent(CollectAndPayMent web)
        {
            return _serviceCollectAndPayMent.Insert(web);
        }
        public int AddDeliveryProduct(DeliveryProduct web)
        {
            return _serviceDeliveryProduct.Insert(web);
        }

        public IEnumerable<DeliveryProduct> GetListDeliveryProducts(Guid invoiceId)
        {
            return _serviceDeliveryProduct.IQueryable(a => a.InvoiceId == invoiceId);
        }


        public int AddSendCar(SendCar web)
        {
            return _serviceSendCar.Insert(web);
        }
        public int AddExpressOrder(ExpressOrder web)
        {
            return _serviceExpressOrder.Insert(web);
        }
        public SendCar GetSingleSendCar(Guid id)
        {
            return _serviceSendCar.FindEntity(a => a.InvoicId == id);
        }

        public SendCar GetSingleSendCarById(Guid id)
        {
            return _serviceSendCar.FindEntity(a => a.Id == id);
        }

        public ExpressOrder GetSingleExpressOrder(Guid id)
        {
            return _serviceExpressOrder.FindEntity(a => a.InvoicId == id);
        }
    }
}
